package com.epalmpay.service.jobservice.impl;

import com.epalmpay.dto.yunliu.memberRedActivity.MemberRedActivityOutDTO;
import com.epalmpay.entity.SysTaskLog;
import com.epalmpay.entity.TradeOffline;
import com.epalmpay.entity.YsMemberRedActivity;
import com.epalmpay.entity.YsMemberRedActivityPerfect;
import com.epalmpay.enumdef.BizEnum;
import com.epalmpay.enumdef.CommonEnum;
import com.epalmpay.enumdef.YunStreamEnum;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.mapper.SysTaskLogMapper;
import com.epalmpay.mapper.TradeOfflineMapper;
import com.epalmpay.mapper.YsMemberRedActivityMapper;
import com.epalmpay.mapper.YsMemberRedActivityPerfectMapper;
import com.epalmpay.service.jobservice.IMemberRedActivityTaskService;
import com.epalmpay.status.Rescode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by GWN on 2018/1/23.
 */
@Service("memberRedActivityTaskService")
public class MemberRedActivityTaskServiceImpl implements IMemberRedActivityTaskService{


    @Resource
    private SysTaskLogMapper sysTaskLogMapper;

    @Resource
    private YsMemberRedActivityPerfectMapper ysMemberRedActivityPerfectMapper;

    @Resource
    private YsMemberRedActivityMapper ysMemberRedActivityMapper;

    @Resource
    private TradeOfflineMapper tradeOfflineMapper;



    public int saveMemberRedActivityPerfectDayTask (Date statisDate) throws ApplicationException{

        int result=0;
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("taskType", CommonEnum.TaskType.MemberRedActivityTask.getType());
        param.put("taskDate", statisDate);

        SysTaskLog oldRecord = sysTaskLogMapper.selectSysTaskLogByTypeAndDate(param);
        if(oldRecord  != null && oldRecord.getResultStatus() == CommonEnum.RunStatus.Success.getType()){
            throw new ApplicationException(Rescode.FAIL,"计划任务已经执行成功！");
        }
        StringBuffer sb = new StringBuffer();

        //返利总金额
        Long amount = 0l;
        int count =0;
        Long id =null;

        //查询正在进行中的活动 及完善资料的会员红利信息
        List<MemberRedActivityOutDTO> memberRedActivityOutDTOList =ysMemberRedActivityMapper.queryMemberRedActivityData();
        for(MemberRedActivityOutDTO memberRedActivityOutDTO :memberRedActivityOutDTOList){
            YsMemberRedActivityPerfect ysMemberRedActivityPerfect = new YsMemberRedActivityPerfect();
            TradeOffline tradeOffline = new TradeOffline();

            ysMemberRedActivityPerfect.setId(memberRedActivityOutDTO.getPid());
            ysMemberRedActivityPerfect.setGmtModified(statisDate);
            id= memberRedActivityOutDTO.getId();

            if(memberRedActivityOutDTO.getRebateGetTime()==1){
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date  currdate = memberRedActivityOutDTO.getGmtCreate();
                Calendar ca = Calendar.getInstance();
                ca.setTime(currdate);
                ca.add(Calendar.HOUR, 2);// num为增加的天数，可以改变的
                currdate = ca.getTime();

                //完善时间+到账时间 与跑批时间比较
                if (currdate.getTime() > statisDate.getTime()) {
                    //未开始 到账时间 显示
                    ysMemberRedActivityPerfect.setStatisDate(null);
                    ysMemberRedActivityPerfect.setAlreadyRebateAmount(0l);
                    ysMemberRedActivityPerfect.setRebateStatus(YunStreamEnum.RebateStatus.stayStart.getType());
                } else if (currdate.getTime() < statisDate.getTime()) {

                    ysMemberRedActivityPerfect.setStatisDate(currdate);
                    ysMemberRedActivityPerfect.setAlreadyRebateAmount(memberRedActivityOutDTO.getOneRebateAmount());
                    ysMemberRedActivityPerfect.setRebateStatus(YunStreamEnum.RebateStatus.over.getType());
                    ysMemberRedActivityPerfect.setIsRebate(1);
                    amount += memberRedActivityOutDTO.getOneRebateAmount();
                    count += 1;

                    tradeOffline.setId(memberRedActivityOutDTO.getTradeId());
                    tradeOffline.setGmtModified(statisDate);
                    tradeOffline.setTransactionStatus((byte) BizEnum.TransactionStatus.Success.getType());
                    tradeOfflineMapper.updateByPrimaryKeySelective(tradeOffline);

                }

            }else if(memberRedActivityOutDTO.getRebateGetTime()==2){
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date  currdate = memberRedActivityOutDTO.getGmtCreate();
                Calendar ca = Calendar.getInstance();
                ca.setTime(currdate);
                ca.add(Calendar.DATE, 1);// num为增加的天数，可以改变的
                currdate = ca.getTime();
                if (currdate.getTime() > statisDate.getTime()) {
                    //未开始 到账时间 显示
                    ysMemberRedActivityPerfect.setStatisDate(null);
                    ysMemberRedActivityPerfect.setAlreadyRebateAmount(0l);
                    ysMemberRedActivityPerfect.setRebateStatus(YunStreamEnum.RebateStatus.stayStart.getType());
                } else if (currdate.getTime() < statisDate.getTime()) {

                    ysMemberRedActivityPerfect.setStatisDate(currdate);
                    ysMemberRedActivityPerfect.setAlreadyRebateAmount(memberRedActivityOutDTO.getOneRebateAmount());
                    ysMemberRedActivityPerfect.setRebateStatus(YunStreamEnum.RebateStatus.over.getType());
                    amount += memberRedActivityOutDTO.getOneRebateAmount();
                    ysMemberRedActivityPerfect.setIsRebate(1);
                    count += 1;

                    tradeOffline.setId(memberRedActivityOutDTO.getTradeId());
                    tradeOffline.setGmtModified(statisDate);
                    tradeOffline.setTransactionStatus((byte) BizEnum.TransactionStatus.Success.getType());
                    tradeOfflineMapper.updateByPrimaryKeySelective(tradeOffline);

                }

            }else if(memberRedActivityOutDTO.getRebateGetTime()==3){

                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date  currdate = memberRedActivityOutDTO.getGmtCreate();
                Calendar ca = Calendar.getInstance();
                ca.setTime(currdate);
                ca.add(Calendar.DATE, 2);// num为增加的天数，可以改变的
                currdate = ca.getTime();
                if (currdate.getTime() > statisDate.getTime()) {
                    //未开始 到账时间 显示
                    ysMemberRedActivityPerfect.setStatisDate(null);
                    ysMemberRedActivityPerfect.setAlreadyRebateAmount(0l);
                    ysMemberRedActivityPerfect.setRebateStatus(YunStreamEnum.RebateStatus.stayStart.getType());
                } else if (currdate.getTime() < statisDate.getTime()) {

                    ysMemberRedActivityPerfect.setStatisDate(currdate);
                    ysMemberRedActivityPerfect.setAlreadyRebateAmount(memberRedActivityOutDTO.getOneRebateAmount());
                    ysMemberRedActivityPerfect.setRebateStatus(YunStreamEnum.RebateStatus.over.getType());
                    ysMemberRedActivityPerfect.setIsRebate(1);
                    amount += memberRedActivityOutDTO.getOneRebateAmount();
                    count += 1;

                    tradeOffline.setId(memberRedActivityOutDTO.getTradeId());
                    tradeOffline.setGmtModified(statisDate);
                    tradeOffline.setTransactionStatus((byte) BizEnum.TransactionStatus.Success.getType());
                    tradeOfflineMapper.updateByPrimaryKeySelective(tradeOffline);

                }

            }else if(memberRedActivityOutDTO.getRebateGetTime()==4){

                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date  currdate = memberRedActivityOutDTO.getGmtCreate();
                Calendar ca = Calendar.getInstance();
                ca.setTime(currdate);
                ca.add(Calendar.DATE, 3);// num为增加的天数，可以改变的
                currdate = ca.getTime();
                if (currdate.getTime() > statisDate.getTime()) {
                    //未开始 到账时间 显示
                    ysMemberRedActivityPerfect.setStatisDate(null);
                    ysMemberRedActivityPerfect.setAlreadyRebateAmount(0l);
                    ysMemberRedActivityPerfect.setRebateStatus(YunStreamEnum.RebateStatus.stayStart.getType());
                } else if (currdate.getTime() < statisDate.getTime()) {

                    ysMemberRedActivityPerfect.setStatisDate(currdate);
                    ysMemberRedActivityPerfect.setAlreadyRebateAmount(memberRedActivityOutDTO.getOneRebateAmount());
                    ysMemberRedActivityPerfect.setRebateStatus(YunStreamEnum.RebateStatus.over.getType());
                    amount += memberRedActivityOutDTO.getOneRebateAmount();
                    ysMemberRedActivityPerfect.setIsRebate(1);
                    count += 1;

                    tradeOffline.setId(memberRedActivityOutDTO.getTradeId());
                    tradeOffline.setGmtModified(statisDate);
                    tradeOffline.setTransactionStatus((byte) BizEnum.TransactionStatus.Success.getType());
                    tradeOfflineMapper.updateByPrimaryKeySelective(tradeOffline);
                }

            }else{

                Date  currdate = memberRedActivityOutDTO.getGmtCreate();
                Calendar ca = Calendar.getInstance();
                ca.setTime(currdate);
                ca.add(Calendar.DATE, 7);// num为增加的天数，可以改变的
                currdate = ca.getTime();
                if (currdate.getTime() > statisDate.getTime()) {
                    //未开始 到账时间 显示
                    ysMemberRedActivityPerfect.setStatisDate(null);
                    ysMemberRedActivityPerfect.setAlreadyRebateAmount(0l);
                    ysMemberRedActivityPerfect.setRebateStatus(YunStreamEnum.RebateStatus.stayStart.getType());
                } else if (currdate.getTime() < statisDate.getTime()) {

                    ysMemberRedActivityPerfect.setStatisDate(currdate);
                    ysMemberRedActivityPerfect.setAlreadyRebateAmount(memberRedActivityOutDTO.getOneRebateAmount());
                    ysMemberRedActivityPerfect.setRebateStatus(YunStreamEnum.RebateStatus.over.getType());
                    amount += memberRedActivityOutDTO.getOneRebateAmount();
                    ysMemberRedActivityPerfect.setIsRebate(1);
                    count += 1;

                    tradeOffline.setId(memberRedActivityOutDTO.getTradeId());
                    tradeOffline.setGmtModified(statisDate);
                    tradeOffline.setTransactionStatus((byte) BizEnum.TransactionStatus.Success.getType());
                    tradeOfflineMapper.updateByPrimaryKeySelective(tradeOffline);

                }

            }
            //更新返利到账金额
            result= ysMemberRedActivityPerfectMapper.updateByPrimaryKeySelective(ysMemberRedActivityPerfect);


            //自动结束活动
            if(id !=null){
                //如果跑批时间大于活动结束时间 结束活动
//                Date dt1 = memberRedActivityOutDTO.getStartTime();
                Date dt2 = memberRedActivityOutDTO.getEndTime();
                if(statisDate.getTime()>dt2.getTime()){
                    YsMemberRedActivity ysMemberRedActivity = new YsMemberRedActivity();
                    ysMemberRedActivity.setId(id);
                    ysMemberRedActivity.setGmtModified(statisDate);
                    ysMemberRedActivity.setRedActivityStatus(YunStreamEnum.MemberRedActivityStatus.over.getType());
                    result = ysMemberRedActivityMapper.updateByPrimaryKeySelective(ysMemberRedActivity);
                }

            }


        }

        if(id !=null){
            YsMemberRedActivity ysMemberRedActivity = new YsMemberRedActivity();
            ysMemberRedActivity.setId(id);
            ysMemberRedActivity.setGmtModified(statisDate);
            ysMemberRedActivity.setRebateAmount(amount);
            ysMemberRedActivity.setRebateSum(count);
            result = ysMemberRedActivityMapper.updateByPrimaryKeySelective(ysMemberRedActivity);
            sb.append("本次任务日统计【会员红利活动返利日统计】影响的行数：").append(result);
        }




        if(oldRecord  == null){
            SysTaskLog record = new SysTaskLog();
            record.setGmtCreate(new Date());
            record.setGmtModified(new Date());
            record.setTaskType(CommonEnum.TaskType.MemberRedActivityTask.getType());
            record.setTaskDate(statisDate);
            record.setResultStatus(CommonEnum.RunStatus.Success.getType());
            record.setRunTimes(0);
            record.setMemo(sb.toString());
            result = sysTaskLogMapper.insert(record);
        }
        else{
            oldRecord.setGmtModified(new Date());
            oldRecord.setResultStatus(CommonEnum.RunStatus.Success.getType());
            oldRecord.setRunTimes((oldRecord.getRunTimes()==null?0:oldRecord.getRunTimes())+1);
            oldRecord.setMemo(sb.toString());
            result = sysTaskLogMapper.updateByPrimaryKeySelective(oldRecord);
        }

        return result;

    }
}
